<?php

Raise::load('core.helper.RaiseHelper');
Raise::load('core.helper.sorting.IRaiseComparator');
Raise::load('RaiseDateTime');

/**
 * RaiseDateTimeComparator class
 * Comparator for RaiseDateTime objects
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.datetime
 * @since 1.2
 */
class RaiseDateTimeComparator extends RaiseHelper implements IRaiseComparator {
    
    /**
     * Compares two RaiseDateTime objects
     * @param RaiseDateTime $date1 First date to compare
     * @param RaiseDateTime $date2 Second date to compare
     * @return integer Return 0 if both dates are equal, 1 if $date2 > $date1 and -1 if $date2 < $date1
     */
    public function compare($date1, $date2){
        if($date1->timezone() != $date2->timezone()){
            $date1 = RaiseDateTime::convertTimezone($date1, $date2->timezone());
        }
        $result = $date1->date()->compareTo($date2->date());
        if($result == 0){
            $result = $date1->time()->compareTo($date2->time());
        }
        return $result;
    }
    
}